/*
   Copyright (c) 2023 李伟国
   learner-platform is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2. 
   You may obtain a copy of Mulan PSL v2 at:
               http://license.coscl.org.cn/MulanPSL2 
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
   See the Mulan PSL v2 for more details. 
*/

package cn.platform.service.impl;

import cn.platform.service.IEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.LinkedHashMap;
import java.util.Map;


@Service
public class EmailServiceImpl implements IEmailService {
    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private TemplateEngine templateEngine;


    @Value("${spring.mail.username}")
    private String fromAddress;

    @Override
    public String getMailConfig() {
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setSubject("subject");
            helper.setFrom(fromAddress);
            helper.setBcc(fromAddress);
            helper.setTo("2590863120@qq.com");
            Context context = new Context();
            context.setVariable("email", "2590863120@qq.com");
            context.setVariable("code","234567");
            String process = templateEngine.process("/active-page.html", context);
            helper.setText(process,true);
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void sendEmail(String context) {
        
    }

    /**
     * 发送激活邮件
     * @param url 激活连接
     * @param toEmail 邮件地址
     */
    @Override
    public void sendEMailActivationUrl(String url, String toEmail) {
        Map<String,String> option = new LinkedHashMap<>();
        option.put("subject","激活邮件——默羽moyu");
        option.put("activeUrl", url);
        option.put("html","/active-page.html");
        try {
            sendEmail(option,toEmail);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送验证邮件
     * @param code 验证码
     * @param toEmail 邮件地址
     */
    @Override
    public void sendEmailVerificationCode(Object code, String toEmail) {
        Map<String,String> option = new LinkedHashMap<>();
        option.put("subject","验证邮件——默羽moyu");
        option.put("code", String.valueOf(code));
        option.put("html","/security-page.html");
        try {
            sendEmail(option,toEmail);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    public void sendEmail(Map<String,String> option,String toEmail) throws MessagingException{
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setSubject(option.get("subject"));
        helper.setFrom(fromAddress);
        helper.setBcc(fromAddress);
        helper.setTo(toEmail);
        Context context = new Context();
        context.setVariable("email", toEmail);
        if(option.containsKey("code")){
            context.setVariable("code",option.get("code"));
        }
        if(option.containsKey("activeUrl")){
            context.setVariable("activeUrl",option.get("activeUrl"));
        }
        String process = templateEngine.process(option.get("html"), context);
        helper.setText(process,true);
        javaMailSender.send(mimeMessage);
    }
}
